local mouQingjian = fk.CreateSkill {
  name = "klmou__qingjian",
}

Fk:loadTranslationTable {
  ["klmou__qingjian"] = "清俭",
  [":klmou__qingjian"] = "当一张牌不因使用而进入弃牌堆后，若你的“清俭”数不大于X（X为你的体力值-1，且至少为1），则你将此牌置于你的武将牌上，" ..
      "称为“清俭”；每名角色的结束阶段开始时，你将你的所有“清俭”牌分配给任意角色。",

  ["$klmou__qingjian1"] = "行俭育德，使下效之。",
  ["$klmou__qingjian2"] = "为公废私，飨恤士卒。",
}

mouQingjian:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self.name) and #player:getPile("klmou__qingjian") < math.max(player.hp - 1, 1) and
    table.find(data, function(info)
      return (info.moveReason ~= fk.ReasonUse or not player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)) and info.toArea == Card.DiscardPile
      and table.find(info.moveInfo, function(moveInfo) return player.room:getCardArea(moveInfo.cardId) == Card.DiscardPile end) ~= nil
    end)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local toPut = {}
    for _, info in ipairs(data) do
      if (info.moveReason ~= fk.ReasonUse or not room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)) and info.toArea == Card.DiscardPile then
        local cardsInDiscardPile = table.filter( info.moveInfo, function(moveInfo) return room:getCardArea(moveInfo.cardId) == Card.DiscardPile end)
        local diff = math.max(player.hp - 1, 1) - #player:getPile("klmou__qingjian")
        if #cardsInDiscardPile > 0 then
          table.insertTable(toPut, table.map(table.slice(cardsInDiscardPile, 1, diff + 1), function(moveInfo) return moveInfo.cardId end))
          if #toPut >= diff then
            break
          end
        end
      end
    end
    player:addToPile("klmou__qingjian", toPut, true, mouQingjian.name)
  end,
})

mouQingjian:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target and player:hasSkill(mouQingjian.name) and target.phase == Player.Finish and #player:getPile("klmou__qingjian") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = #player:getPile("klmou__qingjian")
    room:askToYiji(player, {
      targets = room.alive_players,
      cards = player:getPile("klmou__qingjian"),
      skill_name = mouQingjian.name,
      min_num = num,
      max_num = num,
      expand_pile = "klmou__qingjian"
    })
  end,
})


return mouQingjian
